package com.mz.ozy.activity.shop;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.activity.find.StrategyDetailActivity;
import com.mz.ozy.activity.home.HomeActivity1;
import com.mz.ozy.activity.me.MyOrderActivity;
import com.mz.ozy.activity.me.MyOrderFragment;
import com.mz.ozy.adapter.MallBannerAdapter;
import com.mz.ozy.adapter.MallCenterAdapter;
import com.mz.ozy.adapter.SaveDayAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.HomeBean;
import com.mz.ozy.bean.HomeMerBean;
import com.mz.ozy.bean.MallBean;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MallPresenter;
import com.mz.ozy.mvp.view.MallContract;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.ConfirmPassDialog;
import com.mz.ozy.widget.dialog.ConfirmPayView;
import com.mz.ozy.widget.dialog.OrderConfirmPayDialog;
import com.mz.ozy.widget.dialog.PayPassDialog;
import com.mz.ozy.widget.dialog.PayPassView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MallActivity1 extends BaseActivity<MallContract.View, MallPresenter> implements MallContract.View {

    //    @BindView(R.id.iv_kt_topic)
//    ImageView ivKtTopic;
    @BindView(R.id.rollPagerView)
    RollPagerView rollPagerView;
    @BindView(R.id.iv_bannerPager)
    ImageView ivBannerPager;
    @BindView(R.id.rv_save_days)
    RecyclerView mRvSaveDays;

    private SaveDayAdapter mSaveDayAdapter;
    private MallBannerAdapter mBannerAdapter;
    private List<HomeBean.BannerListBean> mBannerListBeans = new ArrayList<>();
    private boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall1);
        ButterKnife.bind(this);
        titleBar = findViewById(R.id.titleBar);
        setTitleBarMargin();
        initView();
        mPresenter.getMallInfo(this, isFirst);
        isFirst = false;
    }

    @Override
    public void onBackPressed() {
        MainActivity.tabHost.setCurrentTab(0);
    }

    /**
     * 设置沉浸式状态栏时，要把标题栏往下移动状态栏的高度
     */
    protected void setTitleBarMargin() {
        int length = AppUtils.getStatusBarHeight(this);
        //注意布局是哪种Layout
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(titleBar.getLayoutParams());
        lp.setMargins(0, length, 0, 0);
        titleBar.setLayoutParams(lp);
    }

    private void initView() {
        rollPagerView.setAnimationDurtion(500);
        rollPagerView.setHintView(new ColorPointHintView(this, Color.WHITE, getResources().getColor(R.color.c_80ffffff)));
        mBannerAdapter = new MallBannerAdapter(this, mBannerListBeans);
        rollPagerView.setAdapter(mBannerAdapter);
        rollPagerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                if(!Constants.isVip){
//                    showVipDialog();
//                    return;
//                }
                HomeBean.BannerListBean item = mBannerAdapter.getItem(position);
                if (TextUtils.isEmpty(item.getRef())) {
                    return;
                }
                if (item.getType() == 0) {
                    Intent intent = new Intent(MallActivity1.this, ShopDetailActivity.class);
                    intent.putExtra("goodsId", item.getRef());
                    //intent.putExtra("tab", 1);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(MallActivity1.this, StrategyDetailActivity.class);
                    intent.putExtra(StrategyDetailActivity.ARTICLE_ID, Integer.parseInt(item.getRef()));
                    startActivity(intent);
                }
            }
        });

        mRvSaveDays.setLayoutManager(new GridLayoutManager(this, 3));
        mSaveDayAdapter = new SaveDayAdapter(new ArrayList<>());
        mRvSaveDays.setAdapter(mSaveDayAdapter);
        mRvSaveDays.setNestedScrollingEnabled(false);
        mSaveDayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if(!Constants.isVip){
//                    showVipDialog();
//                    return;
//                }
                MallBean.CateListBean bean = mSaveDayAdapter.getItem(position);
                Intent intent = new Intent(MallActivity1.this, SortSearchActivity.class);
                intent.putExtra("id", String.valueOf(bean.getId()));
                intent.putExtra("name", bean.getName());
                intent.putExtra("type", "0");
                startActivity(intent);
            }
        });
    }

    @OnClick({R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_search: {
//                if(!Constants.isVip){
//                    showVipDialog();
//                    return;
//                }
                startActivity(new Intent(this, SearchActivity.class));
//                showSportsHarmDialog();
                break;
            }
        }
    }

    @Override
    public void getMallInfoSuccess(MallBean bean) {
        mBannerListBeans.clear();
        final List<HomeBean.BannerListBean> bannerList = bean.getBannerList();
        if (bannerList != null && bannerList.size() > 0) {
            for (HomeBean.BannerListBean bannerListBean : bannerList) {
                mBannerListBeans.add(bannerListBean);
            }
        } else {
            rollPagerView.setVisibility(View.GONE);
            ivBannerPager.setVisibility(View.VISIBLE);
        }
        mBannerAdapter.notifyDataSetChanged();
        final List<MallBean.CateListBean> cateList = bean.getCateList();
        if (cateList != null && cateList.size() > 0) {
            mSaveDayAdapter.setNewData(cateList);
        }
    }
}
